﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MalRec.Base.DataObject;

namespace MalRec.Base.Controls
{
    /// <summary>
    /// Interaction logic for CachedImage.xaml
    /// </summary>
    public partial class CachedImage : UserControl, INotifyPropertyChanged
    {
        public CachedImage()
        {
            InitializeComponent();
            Stretch = Stretch.UniformToFill;
        }

        private string ImageUrlBehind
        {
            get
            {
                return _ImageUrlBehind;
            }
            set
            {
                if (value != _ImageUrlBehind)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        BackgroundWorker bw = new BackgroundWorker();
                        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                        bw.RunWorkerAsync(value);
                        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                    }
                    _ImageUrlBehind = value;
                }
            }
        }

        public Stretch Stretch
        {
            get;
            set;
        }

        private static void RaiseImageUrlChanged(DependencyObject _do, DependencyPropertyChangedEventArgs e)
        {
            ((CachedImage)_do).ImageUrlBehind = (string)e.NewValue;
        }

        public static readonly DependencyProperty ImageUrlProperty = DependencyProperty.Register("ImageUrl", typeof(string), typeof(CachedImage), new PropertyMetadata("", new PropertyChangedCallback(RaiseImageUrlChanged)));

        public bool ImageUrl
        {
            get { return (bool)GetValue(ImageUrlProperty); }
            set { SetValue(ImageUrlProperty, value); }
        }

        public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(Boolean), typeof(CachedImage), new PropertyMetadata(true));

        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }

        private void RaisePropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            var uri = new Uri(FileCache.FromUrl((string)e.Argument));
            Dispatcher.BeginInvoke((Action)delegate
            {
                try
                {
                    Image.Source = new BitmapImage(uri);
                }
                catch
                {
                }
            });
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IsBusy = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private string _ImageUrlBehind;
    }
}